package com.cxl.price.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cxl.price.bean.Brand;
import com.cxl.price.bean.ItemCat;
import com.cxl.price.dao.BrandDao;
import com.cxl.price.top.service.TaoBaoApiService;
import com.cxl.price.util.RegexpGood;
import com.summer.core.service.impl.BaseServiceImpl;
import com.taobao.api.ApiException;

/**
 * @about: 类目品牌 service 实现
 * @author: wwwlike
 * @create: 2011-07-07
 */
@Service
public class BrandServiceImpl extends BaseServiceImpl<Brand, String, BrandDao>
		implements BrandService {

	@Resource
	public void setDao(BrandDao dao) {
		this.dao = dao;
	}
	
	
	public void addBrands(ItemCat itemCat) throws ApiException
	{
		TaoBaoApiService taobaoApi=new TaoBaoApiService();
		List<Brand> list=taobaoApi.getBrands(itemCat.getCid());
		for(Brand brand:list)
		{
			brand.setItemCat(itemCat);
			dao.save(brand);
		}
	}
	
	public Brand getBrandByCidAndName(Long cid,String title)
	{
		if(title==null)
				return null;
		
		List<Brand> list=dao.getList("itemCat.cid",cid);
		RegexpGood regexp=null;
		for(Brand brand:list)
		{
			regexp=new RegexpGood(brand.getName(), null, title);
			System.out.println("-------"+brand.getName());
			if(regexp.compCN(brand.getName()))
			{
				return brand;
			}
		}
		return null;
	}

}
